import 'package:get/get_connect/http/src/response/response.dart';

import 'auth/ApiBase.dart';
import 'auth/AuthBase.dart';
import 'dio/BaseModel.dart';
import 'dio/DioClient.dart';

class LoginApi extends ApiBase {


  // 登录
  static getLogin(Map map) async {
    return await DioClient().doDio("/login",
        method: HTTPMethod.post, data: map, loading: true);
  }



  //获取验证码
  Future<BaseModel> setCodeRegister(Map<String, dynamic> formData) async {
    BaseModel response =  await DioClient().doDio(
        "/setCodeRegister",
        method: HTTPMethod.post,
        data: formData,
        loading: true
    );
    return response;
  }


  //注册账号
  Future<BaseModel> appRegister(Map<String, dynamic> formData) async {
    BaseModel response =  await DioClient().doDio(
        "/appRegister",
        method: HTTPMethod.post,
        data: formData,
        loading: true
    );

    return response;
  }



  //验证码登录
  Future<BaseModel> codeLogin(Map<String, dynamic> formData) async {
    BaseModel response =  await DioClient().doDio(
        "/codeLogin",
        method: HTTPMethod.post,
        data: formData,
        loading: true
    );

    return response;
  }


  //修改密码
  Future<BaseModel> resetPassword(Map<String, dynamic> formData) async {
    BaseModel response =  await DioClient().doDio(
        "/resetPassword",
        method: HTTPMethod.post,
        data: formData,
        loading: true
    );

    return response;
  }




  Future<BaseModel> loginApp(Map<String, dynamic> formData) async {
    BaseModel response =  await DioClient().doDio(
        "/loginApp",
        method: HTTPMethod.post,
        data: formData,
        loading: true
    );
    return response;
  }




  //注册账号
  Future<BaseModel> register() async {
    BaseModel response =  await DioClient().doDio(
        "/login/register",
        method: HTTPMethod.get,
        loading: true
    );

    return response;
  }



  // 用户登录接口
  Future<Map<String, dynamic>> login(String username, String password) async {
    try {
      final response = await dio.post(
        '/login',
        data: {'username': username, 'password': password},
      );

      if (response.data['code'] == 200) {
        await AuthBase().saveToken(response.data['token']);
      }

      return response.data;
    } catch (e) {
      print('登录异常: $e');
      rethrow;
    }
  }

  // 获取用户信息接口
  Future<Map<String, dynamic>> getUserProfile() async {
    final response = await dio.get('/api/user/profile');
    return response.data;
  }
}